using DeviceCommons.DeviceMessages.Enums;

namespace DeviceCommons.DeviceMessages.Factories
{
    /// <summary>
    /// 设备状态配置接口
    /// 定义设备类型的状态集合构成
    /// </summary>
    public interface IDeviceStateConfiguration
    {
        /// <summary>
        /// 设备类型
        /// </summary>
        byte DeviceType { get; }

        /// <summary>
        /// 状态定义集合
        /// </summary>
        IReadOnlyList<StateDefinition> StateDefinitions { get; }

        /// <summary>
        /// 根据状态ID获取状态定义
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <returns>状态定义，如果不存在则返回null</returns>
        StateDefinition? GetStateDefinition(byte sid);

        /// <summary>
        /// 验证状态值是否有效
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="value">状态值</param>
        /// <returns>是否有效</returns>
        bool ValidateStateValue(byte sid, object value);

        /// <summary>
        /// 验证状态完整性
        /// </summary>
        /// <param name="providedSids">提供的状态ID集合</param>
        /// <returns>验证结果</returns>
        (bool IsValid, IEnumerable<byte> MissingStates) ValidateStateCompleteness(IEnumerable<byte> providedSids);

        /// <summary>
        /// 获取必需的状态定义
        /// </summary>
        /// <returns>必需的状态定义列表</returns>
        IEnumerable<StateDefinition> GetRequiredStates();

        /// <summary>
        /// 检查是否包含所有必需的状态
        /// </summary>
        /// <param name="providedSids">提供的状态ID集合</param>
        /// <returns>是否包含所有必需状态</returns>
        bool ContainsAllRequiredStates(IEnumerable<byte> providedSids);

        /// <summary>
        /// 获取缺失的必需状态
        /// </summary>
        /// <param name="providedSids">提供的状态ID集合</param>
        /// <returns>缺失的必需状态ID列表</returns>
        IEnumerable<byte> GetMissingRequiredStates(IEnumerable<byte> providedSids);
    }

    /// <summary>
    /// 状态定义
    /// </summary>
    public class StateDefinition
    {
        /// <summary>
        /// 状态ID
        /// </summary>
        public byte Sid { get; set; }

        /// <summary>
        /// 状态名称（可选）
        /// </summary>
        public string? Name { get; set; }

        /// <summary>
        /// 状态描述（可选）
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 值类型
        /// </summary>
        public StateValueTypeEnum ValueType { get; set; }

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// 默认值（可选）
        /// </summary>
        public object? DefaultValue { get; set; }

        /// <summary>
        /// 验证规则（可选）
        /// </summary>
        public Func<object, bool>? ValidationRule { get; set; }

        public StateDefinition(byte sid, StateValueTypeEnum valueType)
        {
            Sid = sid;
            ValueType = valueType;
            IsRequired = true;
        }

        public StateDefinition(byte sid, StateValueTypeEnum valueType, string? name = null, string? description = null, bool isRequired = true, object? defaultValue = null, Func<object, bool>? validationRule = null)
        {
            Sid = sid;
            ValueType = valueType;
            Name = name;
            Description = description;
            IsRequired = isRequired;
            DefaultValue = defaultValue;
            ValidationRule = validationRule;
        }
    }
}
